﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;

public partial class Users_AddArticle : BasePage
{

    private string whereStr = "";
    AspNetCs.Bll.Article articleBLL = new AspNetCs.Bll.Article();
    DataTable data = new DataTable();
    protected int ChannelType = -1;
    protected int ChannelTypeId = 0;
    protected int ArticleType = 0;
    private AspNetCs.Bll.Article bll = new AspNetCs.Bll.Article();
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/AddArticle.aspx", "请您登录", true);
        if (ViewState["ChannelType"] != null && ViewState["ChannelTypeId"] != null && ViewState["ArticleType"]!=null)
        {
            ChannelType=StringHelper.isNumber_int(ViewState["ChannelType"].ToString());;
            ChannelTypeId = StringHelper.isNumber_int(ViewState["ChannelTypeId"].ToString());
            ArticleType = StringHelper.isNumber_int(ViewState["ArticleType"].ToString());
        }
        if (!IsPostBack)
        {
            try
            {
                ChannelType = Convert.ToInt32(Request["ChannelType"]);
                ChannelTypeId = Convert.ToInt32(Request["id"]);
                ArticleType = Convert.ToInt32(Request["ArticleType"]);
                ViewState["ChannelType"] = ChannelType;
                ViewState["ChannelTypeId"] = ChannelTypeId;
                ViewState["ArticleType"] = ArticleType;
                BindKindsOfDataControl();
            }
            catch (Exception ex)
            {
            }
        }
    }
    private void BindKindsOfDataControl()
    {
        EnumDescription[] ChannelTypes = EnumDescription.GetFieldTexts(typeof(Channel_Type), EnumDescription.SortType.Rank);  
        //this.ddlChannelType.DataTextField = "EnumDisplayText";
        //this.ddlChannelType.DataValueField = "EnumValue";
        //this.ddlChannelType.DataSource = ChannelTypes;
        //this.ddlChannelType.DataBind();

        //if(ViewState["ChannelType"] !=null)
        //{
        //    this.ddlChannelType.SelectedValue =ViewState["ChannelType"].ToString();
        //}
        

        //BindArticleClass();
    }

    //绑定文章类型
    public void BindArticleClass()
    {
        //DataColumn dc1 = new DataColumn("Classid");
        //DataColumn dc2 = new DataColumn("className");
        //DataColumn dc3 = new DataColumn("parentId");
        //DataColumn dc4 = new DataColumn("ParentPath");
        //DataColumn dc5 = new DataColumn("RootID");
        //DataColumn dc6 = new DataColumn("Child");
        //DataColumn dc7 = new DataColumn("PrevID");
        //DataColumn dc8 = new DataColumn("NextID");
        //DataColumn dc9 = new DataColumn("OrderID");
        //DataColumn dc10 = new DataColumn("Depth");

        //data.Columns.Add(dc1);
        //data.Columns.Add(dc2);
        //data.Columns.Add(dc3);
        //data.Columns.Add(dc4);
        //data.Columns.Add(dc5);
        //data.Columns.Add(dc6);
        //data.Columns.Add(dc7);
        //data.Columns.Add(dc8);
        //data.Columns.Add(dc9);
        //data.Columns.Add(dc10);
        //ddl_NewsClass_Bind(ddlItems, 0);
    } 
    private void ddl_NewsClass_Bind(DropDownList Drop, int ParentID)
    {

        bind_tree(ParentID.ToString());

        string _NewsClass_ID = "-1";
        //dbclass dbc = new dbclass();
        Drop.Items.Add(new ListItem("--请选择--", "-1"));
        //DataTable dt = dbc.GetSql_DataTable("Select * from ArticleClass order by RootID,OrderID") ;

        bool[] arrShowLine = new bool[data.Rows.Count];

        foreach (DataRow dr in data.Rows)
        {
            ArticleClassInfo model = ArticleClass.GetModel(Convert.ToInt32(dr["Classid"].ToString()));

            if (model._NextID > 0)
            {
                arrShowLine[model._Depth] = true;
            }
            else
            {
                arrShowLine[model._Depth] = false;
            }
            string Result = " ";
            if (model._Depth > 0)
            {
                for (int i = 1; i <= model._Depth; i++)
                {
                    if (i == model._Depth)
                    {
                        if (model._NextID > 0)
                        {
                            Result += "├";
                        }
                        else
                        {
                            Result += "└";
                        }
                    }
                    else
                    {
                        if (arrShowLine[i] == true)
                        {
                            Result += "│";
                        }
                        else
                        {
                            Result += "　";
                        }
                    }
                }
            }
            if (model._LinkUrl == "" && model._Child == 0)
            {
                _NewsClass_ID = model._Classid.ToString();
                Result += model._className + "【可选项】";
            }
            else
            {
                _NewsClass_ID = "-1";
                Result += model._className;
            }
            ListItem Item = new ListItem();
            Item.Text = Result;
            Item.Value = _NewsClass_ID;
            Drop.Items.Add(Item);
        }
    }

    private void bind_tree(string ChildNodes)
    {
        //绑定根节点
        dbclass dbc = new dbclass();
        DataTable dt = dbc.GetSql_DataTable("select * from ArticleClass where parentId=" + ChildNodes + " order by OrderID ");

        foreach (DataRow dr in dt.Rows)
        {
            DataRow dar = data.NewRow();
            dar["Classid"] = dr["Classid"];
            dar["className"] = dr["className"];
            dar["parentId"] = dr["parentId"];
            dar["ParentPath"] = dr["ParentPath"];
            dar["RootID"] = dr["RootID"];
            dar["Child"] = dr["Child"];
            dar["PrevID"] = dr["PrevID"];
            dar["NextID"] = dr["NextID"];
            dar["OrderID"] = dr["OrderID"];
            dar["Depth"] = dr["Depth"];
            data.Rows.Add(dar);

            bind_tree(dr["Classid"].ToString());
        }
    }


    //从界面获得文章实体
    private Article_Model getModel()
    {
        Article_Model model = new Article_Model();
        int IsHot = 0;
        int IsTop = 0;
        int IsCommend = 0;


        int ClassId = ArticleType;
        int MaxCharPerPage = 0;
        int IsIncludePic = 0;

        int IsPassed = configInfo.ArticleCheck==0?1:0;
        
        int ReadScore = 0;
        int TitleFontType =StringHelper.isNumber_int(txtTitleFont.Text) ;
        int TitleFontBlod = 0;
        int TitleFontIta = 0;

        if (chkPicture.Checked)
        {
            IsIncludePic = 1;
        }
        if (chkTitleFontBlod.Checked)
        {
            TitleFontBlod = 1;
        }
        if (chkTitleFontIta.Checked)
        {
            TitleFontIta = 1;
        }
        model.Title = txtArticleTitle.Text;
        model.Author = txtAuthor.Text;
        model.CopyFrom = txtCopyFrom.Text;
        model.IsMaster = 0;

        //用户编号
        Users_Model userModel = Base_GetUserInfo;
        if(userModel.UserId!=0)
        {
            model.EditorId = userModel.UserId;
        }

        model.KeyWord = txtKeyWord.Text;
        model.Hits = 0;
        model.UpdateTime = DateTime.Now;
        model.IsHot = IsHot;
        model.IsTop = IsTop;
        model.IsCommend = IsCommend;
        model.IsPassed = IsPassed;
        model.Content = WebEditor1.Text;
        model.IncludePic = IsIncludePic;
        model.DefaultPicUrl = Txt_UpImg.Text;
        model.ReadScore = ReadScore;
        model.ClassID = ClassId;
        model.IsDel = 0;
        model.TitleFontColor = txtTitleFontColor.Value;
        model.TitleFontType = TitleFontType;
        model.TitleFontBlod = TitleFontBlod;
        model.TitleFontIta = TitleFontIta;
        model.MaxCharPerPage = MaxCharPerPage;
        model.AppraiseNum = 0;
        if (ViewState["ChannelType"]!=null&&ViewState["ChannelTypeId"]!=null&&ViewState["ArticleType"]!=null)
        {
            ChannelType = StringHelper.isNumber_int(ViewState["ChannelType"].ToString());
            ChannelTypeId = StringHelper.isNumber_int(ViewState["ChannelTypeId"].ToString());
            ArticleType = StringHelper.isNumber_int(ViewState["ArticleType"].ToString());
        }
        model.ChannelTypeID = ChannelTypeId;
        model.ChannelType = ChannelType;
        return model;
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        bll.AddArticle(getModel());
        ShowMsgBackParent("添加成功!", "ArticleList_" + ChannelTypeId + "_" + ChannelType + "_" + ArticleType + ".aspx");
    }
}
